package com.example.yeujian5.yuejian5.model;

import com.example.yeujian5.yuejian5.Interface.MasterFace;
import com.example.yeujian5.yuejian5.Listenner.MasterDetailListener;
import com.example.yeujian5.yuejian5.Listenner.MasterListener;
import com.example.yeujian5.yuejian5.bean.MasterBean;
import com.example.yeujian5.yuejian5.service.MasterService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangmengmeng on 2017/10/10.
 */

public class MasterModel implements MasterFace {

    private Retrofit retrofit;
    private MasterService masterService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public MasterModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获得大师列表
     */
    @Override
    public void getList(String id, String realname,final MasterListener listListener) {
        masterService = retrofit.create(MasterService.class);
        Call<List<MasterBean>> call =masterService.getMasterList();
        call.enqueue(new Callback<List<MasterBean>>() {
            @Override
            public void onResponse(Call<List<MasterBean>> call, Response<List<MasterBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<MasterBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    /**
     * 获得大师详情
     */


    @Override
    public void getMasterById( String id,final MasterDetailListener listener) {
        masterService = retrofit.create(MasterService.class);
        Call<MasterBean> call= masterService.getMasterById(id);
        call.enqueue(new Callback<MasterBean>() {
            @Override
            public void onResponse(Call<MasterBean> call, Response<MasterBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<MasterBean> call, Throwable t) {

            }
        });
    }


    //收藏

    public Call isCollected(String fan_id,int userid){
        MasterService service = retrofit.create(MasterService.class);
        return service.isCollected(fan_id,userid,"2","lgj98rfkiqvenhrk2o7oe2po81");
    }

    public Call userCollectMaster(int userid,String fan_id) {
        MasterService service = retrofit.create(MasterService.class);
        return service.userCollectMaster("lgj98rfkiqvenhrk2o7oe2po81",userid,fan_id,"2");
    }

    public Call changeTeaState1(int userid,String fan_id) {
        MasterService service = retrofit.create(MasterService.class);
        return service.changeTeaState1(userid,fan_id,"2");
    }



    //添加提问
    public Call addQuestion(String master_id, int user_id,String question){
        MasterService service = retrofit.create(MasterService.class);
        return service.addQuestion(master_id,user_id,question);
    }




}


